<template>
  <div class="app-container">
    <el-form
      ref="dataForm"
      :rules="rules"
      :model="postForm"
      label-position="right"
      label-width="100px"
      style="width: 600px; margin-left:50px;"
    >
      <el-form-item label="id" prop="id" hidden>
        <el-input v-model="postForm.id" clearable placeholder="id" maxlength="8" :disabled="formDisabled" />
      </el-form-item>
      <el-form-item label="企业编码" prop="entCode">
        <el-input v-model="postForm.entCode" clearable placeholder="企业编码" maxlength="10" :disabled="formDisabled" />
      </el-form-item>
      <el-form-item label="企业名称" prop="entName">
        <el-input v-model="postForm.entName" clearable placeholder="企业名称" maxlength="20" :disabled="formDisabled" />
      </el-form-item>
      <el-form-item>
        <el-button @click="back()">返回</el-button>
        <el-button v-if="submitHidden===false" type="primary" @click="submitForm">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addDicInfo, updateDicInfo, getDicInfo } from '@/api/dic/info'
import msgUtil from '@/utils/msgUtil'
import routerUtil from '@/utils/routerUtil'

const defaultForm = {
  id: null, entCode: null, entName: null
}

export default {
  name: 'DicInfoDetail',
  props: {
    operaType: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      id: this.$route.query.id,
      postForm: Object.assign({}, defaultForm),
      formDisabled: false, // 表单是否禁用
      submitHidden: false, // 提交按钮是否隐藏
      rules: {
        entName: [{ required: true, trigger: 'blur', message: '企业名称不能为空' }]
      }
    }
  },
  created() {
    if (this.operaType === 2) {
      this.getDicInfo(this.id)
    } else if (this.operaType === 3) {
      this.getDicInfo(this.id)
      this.formDisabled = true
      this.submitHidden = true
    } else {
      this.loading = false
    }
  },
  methods: {
    // 修改
    updateDicInfo() {
      updateDicInfo(this.postForm).then(response => {
        msgUtil.success()
        routerUtil.backPath(this, '/dic/info/index')
      })
    },
    // 新增
    addDicInfo() {
      addDicInfo(this.postForm).then(response => {
        msgUtil.success()
        routerUtil.backPath(this, '/dic/info/index')
      })
    },
    // 返回
    back() {
      routerUtil.backPath(this, '/dic/info/index')
    },
    // 提交form
    submitForm() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          if (this.operaType === 2) {
            this.updateDicInfo()
          } else if (this.operaType === 1) {
            this.addDicInfo()
          }
        }
      })
    },
    // 获取角色信息并赋值给当前页面
    getDicInfo(id) {
      getDicInfo({ id: id }).then(response => {
        this.postForm = response.data
      })
    }
  }
}
</script>
